//
//  HandleAppointmentViewController.m
//  WeiWeiApp
//
//  Created by 高继鹏 on 15/10/15.
//  Copyright © 2015年 GaoJipeng. All rights reserved.
//

#import "HandleAppointmentViewController.h"
#import "HandleAppointCell.h"
#import "ConnectViewController.h"

@interface HandleAppointmentViewController (){
    UIView *upperSelectView;
    NSMutableArray *tableSource;
    UITableView *_tableView;
    NSInteger tempBtnTag;
    CGRect Allframe;
    NSArray *topArr;
    NSString *phoneNum;
    UIWebView *_phoneCallWebView;
}

@end

@implementation HandleAppointmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleWithSting:@"处理预约"];
    [self addBtnOnNavWithTitle:@"我" andImageName:@"返回" andTarget:self andAction:@selector(backAction) andFrame:CGRectMake(0, 0, 45, 20) andDirection:left];
    Allframe = [UIScreen mainScreen].bounds;
    [self createUI];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 35, Allframe.size.width, Allframe.size.height-60) style:UITableViewStylePlain];
    _tableView.backgroundColor = BACKGROUDCOLOR;
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HandleAppointCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([HandleAppointCell class])];
    [self.view addSubview:_tableView];
    [self createData];
}

-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createData
{
    tableSource = [[NSMutableArray alloc] init];
    [tableSource addObject:@[@"威威商城昵称",@"2015年9月5日 13:30",@"平安里分店",@"黄晓明",@"1",@"13849961142"]];
    [tableSource addObject:@[@"威威商城昵称",@"2015年7月19日 18:30",@"宝盛里分店",@"郭德纲",@"1",@"15632675880"]];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView reloadData];
}

- (void)createUI
{
    topArr = @[@"待处理",@"已完成",@"已爽约",@"已接受"];
    upperSelectView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Allframe.size.width, 35)];
    upperSelectView.backgroundColor = [UIColor whiteColor];
    UILabel *lineLow = [[UILabel alloc] initWithFrame:CGRectMake(0, 34.5f, WIDTH, 0.5f)];
    lineLow.backgroundColor = groupDarkGrayColor;
    [upperSelectView addSubview:lineLow];
    [self.view addSubview:upperSelectView];
    int i = 0;
    tempBtnTag = 0+1000;
    CGFloat width = Allframe.size.width/4;
    NSLog(@"width:%f",width);
    for (NSString *titleStr in topArr) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i*width, 0, width, 35);
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitle:titleStr forState:UIControlStateNormal];
        [button setTitleColor:OneFiveThreeColor forState:UIControlStateNormal];
        if (i == 0) {
            [button setTitleColor:[UIColor colorWithRed:181.0/255.0 green:89.0/255.0 blue:234.0/255.0 alpha:1.0] forState:UIControlStateNormal];
        }
        button.tag = i+1000;
        [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [upperSelectView addSubview:button];
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(width*i, 0, 0.3f, 35)];
        line.backgroundColor = [UIColor lightGrayColor];
        if (i!=0) {
            [upperSelectView addSubview:line];
        }
        i++;
    }
    
    
}

- (void)btnAction:(UIButton *)sender
{
    NSLog(@"sender:%ld",(long)sender.tag);
    
    UIButton *tempBtn = (UIButton *)[upperSelectView viewWithTag:tempBtnTag];
    [tempBtn setTitleColor:OneFiveThreeColor forState:UIControlStateNormal];
    
    [sender setTitleColor:[UIColor colorWithRed:181.0/255.0 green:89.0/255.0 blue:234.0/255.0 alpha:1.0] forState:UIControlStateNormal];
    tempBtnTag = sender.tag;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 161;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tableSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HandleAppointCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HandleAppointCell class]) forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.contentView.backgroundColor = BACKGROUDCOLOR;
    cell.statusLabel.textColor = OneFiveThreeColor;
    cell.dateLabel.textColor = OneOTwoColor;
    cell.addressLabel.textColor  = OneOTwoColor;
    cell.nameLabel.textColor     = OneOTwoColor;
    
    if (tableSource.count != 0) {
        NSInteger tempIndex = [tableSource[indexPath.row][4] integerValue];
        cell.statusLabel.text   = topArr[tempIndex];
        cell.shopNameLabel.text = tableSource[indexPath.row][0];
        cell.dateLabel.text     = tableSource[indexPath.row][1];
        cell.addressLabel.text  = tableSource[indexPath.row][2];
        cell.nameLabel.text     = tableSource[indexPath.row][3];
        cell.phoneNum = tableSource[indexPath.row][5];
        [cell.connectBtn addTarget:self action:@selector(phoneAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return cell;
}
   
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (void)phoneAction:(UIButton *)sender
{
    HandleAppointCell *cell = (HandleAppointCell*)[[[sender superview] superview] superview];
    NSIndexPath *path = [_tableView indexPathForCell:cell];
    NSMutableArray *tempArr = [[NSMutableArray alloc] init];
    [tempArr addObject:tableSource[path.row][0]];
    [tempArr addObject:tableSource[path.row][5]];
    ConnectViewController *connectVc = [[ConnectViewController alloc] init];
    connectVc.infoDate = tempArr;
    [self.navigationController pushViewController:connectVc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
